/*
 *  txdnet.cn tonydon
 * 
 */
package cn.txdnet.txweb.utils;

import java.io.BufferedInputStream;
import java.io.DataOutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import txdnet.util.DataUtil;
import txdnet.util.Logger;
import txdnet.util.StaticMark;

/**
 * http request util
 *
 * @author txdnet
 */
public class HttpUtil {

    /**
     * 简单的Get请求
     *
     * @param siteUrl
     * @param charset
     * @param connectTimeout
     * @param readTimeout
     * @return String
     *
     */
    public static String doGet(String siteUrl, String charset,
            Integer connectTimeout, Integer readTimeout) {
        String content = StaticMark.STR_EMPTY;
        BufferedInputStream bis = null;
        HttpURLConnection urlconn = null;
        try {
            urlconn = (HttpURLConnection) (new URL(siteUrl)).openConnection();
            setRequestProperty(urlconn, "GET", charset, connectTimeout, readTimeout);
            urlconn.connect();
            if (urlconn.getResponseCode() == HttpURLConnection.HTTP_OK) {
                bis = new BufferedInputStream(urlconn.getInputStream());
                content = IOUtils.toString(bis, charset);
            }
        } catch (Exception e) {
            Logger.warn(HttpUtil.class, e.toString());
        } finally {
            IOUtils.closeQuietly(bis);
            if (urlconn != null) {
                urlconn.disconnect();
            }
        }
        return content;
    }

    /**
     * 简单的POST请求
     *
     * @param postUrl
     * @param charset
     * @param params
     * @param connectTimeout
     * @param readTimeout
     * @return String
     */
    public static String doPost(String postUrl, String charset, Map params,
            Integer connectTimeout, Integer readTimeout) {
        String content = StaticMark.STR_EMPTY;
        BufferedInputStream bis = null;
        DataOutputStream dos = null;
        HttpURLConnection urlconn = null;
        try {
            urlconn = (HttpURLConnection) (new URL(postUrl)).openConnection();
            urlconn.setInstanceFollowRedirects(true);
            setRequestProperty(urlconn, "POST", charset, connectTimeout, readTimeout);
            urlconn.connect();
            dos = new DataOutputStream(urlconn.getOutputStream());
            dos.writeBytes(DataUtil.parseQueryString(params));
            dos.flush();
            dos.close();
            if (urlconn.getResponseCode() == HttpURLConnection.HTTP_OK) {
                bis = new BufferedInputStream(urlconn.getInputStream());
                content = IOUtils.toString(bis, charset);
            }
        } catch (Exception e) {
            Logger.warn(HttpUtil.class, e.toString());
        } finally {
            IOUtils.closeQuietly(dos);
            IOUtils.closeQuietly(bis);
            if (urlconn != null) {
                urlconn.disconnect();
            }
        }
        return content;
    }

    /**
     * 设置缺省属性
     *
     */
    private static void setRequestProperty(HttpURLConnection urlconn, String reqMethod,
            String charset, Integer connectTimeout, Integer readTimeout) throws Exception {
        int timeout = 8000;
        urlconn.setConnectTimeout(connectTimeout == null ? timeout : connectTimeout);
        urlconn.setReadTimeout(readTimeout == null ? timeout : readTimeout);
        urlconn.setDoOutput(true);
        urlconn.setDoInput(true);
        urlconn.setUseCaches(false);
        urlconn.setRequestMethod(reqMethod);
        urlconn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=".concat(charset));
        urlconn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 5.1; rv:16.0) Gecko/20100101 Firefox/16.0");
        urlconn.setRequestProperty("Accept", "*/*");
        urlconn.setRequestProperty("Accept-Language", "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3");
    }
}
